<?php

/**
 * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 * Copyright (c) 2010 Mr. Ynk: http://www.mrynk.nl
 */

/**
 * @author Michel Ypma: michel@mrynk.nl
 */

class MytoCore
{

    private $__config;
    private $__cli;
    private $__manager;

    /**
     *
     */
    public function __construct ( )
    {}

    /**
     *
     */
    public function loadConfig ( $_file )
    {
        $this->__config = parse_ini_file( $_file );

		if ( !defined('PATH_PHP_LIB') )
			define('PATH_PHP_LIB', $this->__config['myto_core'].'../' );

		define('MYTO_LIB', $this->__config['myto_core'] );
        define('MYTO_SPYC', $this->__config['myto_spyc'] );
        define('MYTO_CLI', $this->__config['myto_cli'] );
        define('MYTO_SCHEMA', $this->__config['myto_schema'] );
        ini_set('include_path',
            PATH_PHP_LIB . PATH_SEPARATOR .
			MYTO_LIB . PATH_SEPARATOR .
            MYTO_SPYC . PATH_SEPARATOR .
            MYTO_CLI . PATH_SEPARATOR .
            ini_get('include_path')
        );
    }

    public function startService()
    {
        require_once( 'cli/MytoCLI.php' );
        require_once( 'builder/BuildManager.php' );
        $this->__manager = new BuildManager();
        $this->__cli = new MytoCLI( $this );
        $this->__cli->start();
    }

    public function execute( $options, $args )
    {
        $functional = false;
        foreach( $options as $c => $option )
            if ( !empty( $option ) )
                $functional = true;
        if ( !$functional )
            throw new Exception( 'Myto needs options before it can function properly', 1 );

        if ( !empty( $options['gen'] ) )
        {
            $this->initGenerator( $options['gen'], $options['class'] );
        }
    }

    public function initGenerator( $defs, $_classes = false )
    {
        require_once 'schema/SchemaLoader.php';
        require_once 'builder/ClassBuilder.php';

        MytoCLI::output( PHP_EOL.'|---------------------------|' );
        MytoCLI::output( '| ::: Myto v2.0 started ::: |' );
        MytoCLI::output( '|---------------------------|' );

        $this->__manager->loadSchemas( $defs );
        $this->__manager->buildClasses( $_classes );

        MytoCLI::output( ':: MYTO FINISHED' );
    }
}

?>